#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int i;

main (char **argvp){

	int check[4];

	for(i=0; argvp[i]!=NULL; i++){
		if(!strcmp(argvp[i], "<"))
			check[0]=1;
		else if(!strcmp(argvp[i], "<<"))
			check[1]=1;
		else if(!strcmp(argvp[i], ">"))
			check[2]=1;
		else if(!strcmp(argvp[i], ">>"))
			check[3]=1;
	}
		
	if((check[0]==1)||(check[1]==1))
		redirectionout(argvp, check[1]);
	//if((check[2]==1)||(check[3]==1))
	//	redirectionin(argvp);
		
}

/*void redirectionout(char **argvp){
	
	int ofd, i;
	
	for(i=0; argvp[i]!=NULL; i++){
		if((!strcmp(argvp[i], "<")||(!strcmp(argvp[i], "<<"))
			break;
	}
	if((!strcmp(argvp[1] "<")&&
}*/

void redirectionout(char **argvp){
	
	int outfd;
		
	for(i=0; argvp[i+1]!=NULL; i++){
		if((!strcmp(argvp[i], ">"))||(!strcmp(argvp[i], ">>")))
			break;
	}
	
	if(outfd=open(argvp[i+1], O_CREAT)){ // If file don't exist, create the new file
		perror("cant.....");	
	}
/*
	else if((!strcmp(argvp[i], ">"))&&(argvp[i+1]!=NULL)){
		outfd=open(argvp[i+1],O_TRUNC | O_CREAT); // If file exist, delete the file and create new file
	}
	else if((!strcmp(argvp[i], ">>"))&&(argvp[i+1]=NULL)){
		outfd=open(argvp[i+1], O_CREAT);
	}
	else if((!strcmp(argvp[i], ">>"))&&(argvp[i+1]!=NULL)){
		outfd=open(argvp[i+1], O_APPEND);
	}
*/
	if(dup2(outfd, STDOUT_FILENO)==-1){
		perror("Can't duplicate...");
	}
	close(outfd);	
}
